
package mapper;

import Utilities.DBConnection;
import dto.DTO_KhachHang;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MAP_KhachHang {
    public boolean TrungTenDangNhap(String uname)
    {
        boolean flag = false;
        try {
            String qry = "select * from KhachHang where TenDangNhap = '"+uname+"'";
            ResultSet rs = DBConnection.getResultSet(qry);
            if(rs != null && rs.next())
            {
                flag = true;

            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_KhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }
        return flag;
    }
    public boolean KiemTraDangNhap(String uname,String pass)
    {
        boolean flag = false;
        try {
            String qry = "select * from KhachHang where TenDangNhap = '"+uname + "' and MatKhau = '"+pass+"'";
            ResultSet rs = DBConnection.getResultSet(qry);
            if(rs != null && rs.next())
            {
                flag = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_KhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }
        return flag;

    }
    public boolean DoiMatKhau(String TenDangNhap,String MatKhau)
    {
        String qry = "update KhachHang set MatKhau = '"+MatKhau+"' where TenDangNhap = '"+TenDangNhap+"'";
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        return false;

    }
    public boolean ThemKH(DTO_KhachHang kh)
    {

        String qry = "insert into KhachHang(TenDangNhap,MatKhau,HoTen,GioiTinh,DiaChi,Email,SoDienThoai,isRole) values(N'"+kh.getTenDangNhap()+"',N'"+kh.getMatkhau()+"',N'"+kh.getHoTen()+"',"+kh.getGioiTinh()+",N'"+kh.getDiaChi()+"',N'"+kh.getEmail()+"',N'"+kh.getDienThoai()+"',0)";
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public DTO_KhachHang LayThongTinKhachHang(String uname)
    {
        DTO_KhachHang dtoKH = new DTO_KhachHang();
        try {
            String qry = "select * from KhachHang where TenDangNhap = '"+uname+"'";
            ResultSet rs = DBConnection.getResultSet(qry);
            if(rs != null && rs.next())
            {
                initKH(dtoKH, rs);

            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_KhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }
        return dtoKH;
    }
    public void initKH(DTO_KhachHang kh , ResultSet rs)
    {
        try {
            kh.setMaKH(rs.getInt(1));
            kh.setTenDangNhap(rs.getString(2));
            kh.setMatkhau(rs.getString(3));
            kh.setHoTen(rs.getString(4));
            kh.setGioiTinh(rs.getInt(5));
            kh.setDiaChi(rs.getString(6));
            kh.setEmail(rs.getString(7));
            kh.setDienThoai(rs.getString(8));
            kh.setQuyenHan(rs.getInt(9));


        } catch (SQLException ex) {
            Logger.getLogger(MAP_KhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    public boolean CapNhatKH(DTO_KhachHang kh)
    {

        String qry = "Update KhachHang Set HoTen = '"+kh.getHoTen()+"' ,GioiTinh = "+kh.getGioiTinh()+" ,DiaChi = '"+kh.getDiaChi()+"' ,Email = '"+kh.getEmail()+"' ,SoDienThoai = '"+kh.getDienThoai()+"'"+
                " where TenDangNhap = '"+kh.getTenDangNhap()+"'";
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;

        }
        else
        {
            return false;
        }
    }
    public boolean KiemTraEmail(String tendangnhap,String email)
    {
        boolean flag = false;
        try {
            String qry = "select * from KhachHang where tendangnhap = '" + tendangnhap + "' and Email = '" + email + "'";
            ResultSet rs = DBConnection.getResultSet(qry);
            if (rs != null && rs.next()) {
                flag = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_KhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }
        return flag;

    }
    public int LaQuanLy(String TenDangNhap)
    {
        String qry = "select isRole from KhachHang where TenDangNhap = '"+TenDangNhap+"'";
        ResultSet rs = DBConnection.getResultSet(qry);
        try {
            if (rs.next() && rs != null) {
                return 1;
            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_KhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;

    }
     public ArrayList<DTO_KhachHang> LayDanhSachKHTheoMaKH(int MaKH)
    {
        ArrayList<DTO_KhachHang> ds = new ArrayList<DTO_KhachHang>();

        String qry = "SELECT * from khachhang where makh =  "+MaKH;
        ResultSet rs = DBConnection.getResultSet(qry);
        try {
            while (rs != null && rs.next()) {
                DTO_KhachHang kh = new DTO_KhachHang();
                initKH(kh, rs);
                ds.add(kh);
            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_KhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }
    public ArrayList<DTO_KhachHang> LayDanhSachKHTheoTenKH(String TenKH)
    {
        ArrayList<DTO_KhachHang> ds = new ArrayList<DTO_KhachHang>();
        String qry = "SELECT * from khachhang where HoTen like  '%"+TenKH+"%'";
        ResultSet rs = DBConnection.getResultSet(qry);
         try {
            while (rs != null && rs.next()) {
                DTO_KhachHang kh = new DTO_KhachHang();
                initKH(kh, rs);
                ds.add(kh);
            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_KhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }
    public ArrayList<DTO_KhachHang> LayDanhSachKH()
    {
        ArrayList<DTO_KhachHang> ds = new ArrayList<DTO_KhachHang>();
        String qry = "SELECT * from khachhang ";
        ResultSet rs = DBConnection.getResultSet(qry);
         try {
            while (rs != null && rs.next()) {
                DTO_KhachHang kh = new DTO_KhachHang();
                initKH(kh, rs);
                ds.add(kh);
            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_KhachHang.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }
    public boolean XoaKhachHang(int MaKH)
    {
        String qry = "delete from KhachHang where MaKH = "+MaKH;
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        return false;

    }
    public boolean CapNhatKHQL(DTO_KhachHang kh)
    {
          String qry = "Update KhachHang Set MatKhau = ' "+ kh.getMatkhau()+"' ,HoTen = '"+kh.getHoTen()+"' ,GioiTinh = "+kh.getGioiTinh()+" ,DiaChi = '"+kh.getDiaChi()+"' ,Email = '"+kh.getEmail()+"' ,SoDienThoai = '"+kh.getDienThoai()+"'"+
                ",isRole = " + kh.getQuyenHan() + " where MaKH = "+kh.getMaKH()+"";
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;

        }
        else
        {
            return false;
        }

    }
}
